package com.center.growcenterserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.center.growcenterserver.common.JwtUtils;
import com.center.growcenterserver.common.R;
import com.center.growcenterserver.mapper.TeacherMapper;
import com.center.growcenterserver.model.domain.Admin;
import com.center.growcenterserver.model.domain.Teacher;
import com.center.growcenterserver.model.request.AdminRequest;
import com.center.growcenterserver.model.request.TeacherRequest;
import com.center.growcenterserver.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 学生服务实现类
 * </p>
 *
 * @author niit
 */
@Slf4j
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public R allTeachers(IPage<Teacher> page) {
        IPage<Teacher> selectPage = teacherMapper.selectPage(page, null);
        return R.success("查询成功", selectPage);
    }

    @Override
    public R deleteTeacherById(String id) {
        int ret = teacherMapper.deleteById(id);
        if (ret == 1) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R deleteTeacherByIds(String[] ids) {
        if (ids == null) {
            // 如果参数列表为空，直接返回 0 表示没有删除任何记录
            return R.error("请选择要删除的对象");
        }
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("teacher_id", ids);
        int ret = teacherMapper.delete(queryWrapper);
        if (ret > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R selectTeacherInfo() {
        // 查询教师总数
        Long teacherTotalCount = teacherMapper.selectCount(null); // null 参数表示没有额外的查询条件
        // 查询不同专业的教师数量
        List<Map<String, Object>> teacherCountByMajor = teacherMapper.selectTeacherCountByMajor();

        // 将结果放入Map中
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("teacherTotalCount", teacherTotalCount);
        resultMap.put("teacherCountByMajor", teacherCountByMajor);
        // 返回成功响应及数据
        return R.success("查询成功", resultMap);
    }


    @Override
    public int updateTeacherImage(String id, String picUrl) {
        Teacher teacher = teacherMapper.selectById(id);
        if (teacher == null) {
            throw new RuntimeException("Teacher not found with id: " + id);
        }
        teacher.setPic(picUrl);
        return teacherMapper.updateById(teacher);
    }

    @Override
    public int addTeacher(Teacher teacher) {
        return teacherMapper.insert(teacher);
    }

    @Override
    public R updateTeacher(TeacherRequest teacherRequest) {
        Teacher Teacher = new Teacher();
        BeanUtils.copyProperties(teacherRequest, Teacher);
        if (teacherMapper.updateById(Teacher) > 0) {
            return R.success("学生信息更新成功", Teacher);
        } else {
            return R.fatal("学生信息更新失败");
        }
    }


    @Override
    public R verityPasswd(TeacherRequest teacherRequest, HttpSession session) {
        String username = teacherRequest.getRealName();
        String password = teacherRequest.getPassword();

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("real_name", teacherRequest.getRealName());
        queryWrapper.eq("password", teacherRequest.getPassword());
        if (teacherMapper.selectCount(queryWrapper) > 0) {
//            session.setAttribute("name", adminRequest.getUsername());
            String tokenStr = JwtUtils.generateToken(teacherRequest.getRealName());

            // 查询用户信息，假设username是唯一的，所以使用eq进行精确匹配
            Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("real_name", username));

            // 封装用户信息和token到一个Map或自定义对象中，以便一并返回
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("token", tokenStr);
            resultMap.put("userInfo", teacher);

            return R.success("登录成功",resultMap);
        } else {
            return R.error("用户名或密码错误");
        }
    }


}